#include <basic.h>
#include <prot.h>

BOLT(mtool_show, "mtool show")
{
   mtool_show();
}

BOLT(mtool_sd_num, "mtool <id> send <number>")
{
   uint32_t num, id;
   int ret;
   
   if (!str2u32(pop_argz(), &num)){
      printf("invalid number\n");
      return;
   } 
   if (!str2u32(pop_argz(), &id)){
      printf("invalid id\n");
      return;
   }   
   for(ret=0;num>0;num--)
      if (mtool_send((int )id) > 0)
         ret++;
   printf("mtool send num=%d\n", ret);
}


BOLT(mtool_release, "mtool <id> release")
{
   uint32_t id;
   if (!str2u32(pop_argz(), &id)){
      printf("invalid id\n");
      return;
   } 
   mtool_release((int)id);
}

BOLT(mtool_dscp, "mtool <id> dscp dscp-value>")
{
   uint8_t dscp = 0;
   uint32_t id;
   if (!str2u8(pop_argz(), &dscp) || dscp > 63){
      printf("invalid DSCP\n");
      return;
   }
   if (str2u32(pop_argz(), &id))
      printf("Set DSCP %u:ret=%d\n", dscp, mtool_set_dscp((int)id, dscp));
   else
      printf("invalid DSCP\n");
}

BOLT(mt_create, "mtool <id> create <src-ip> <src-port> <dst-ip> <dst-port>")
{
   uint32_t mt_id;
   sockaddr_t src, dst;
   uint16_t port;
   int id;
   
   memset(&src, 0, sizeof(src));
   memset(&dst, 0, sizeof(dst));
   if (!str2u16(pop_argz(), &port)){
      printf("invalid destination port\n");
      return;
   } 
   if (!ip2addr(pop_argz(), &dst.v0)) {
      printf ("Invalid destination IP.\n");
      return;
   }
   addr_set_port(&dst, port);
   if (!str2u16(pop_argz(), &port)){
      printf("Invalid source port\n");
      return;
   } 
   if (!ip2addr(pop_argz(), &src.v0)) {
      printf ("Invalid source IP.\n");
      return;
   }
   addr_set_port(&src, port);
   if (!str2u32(pop_argz(), &mt_id)){
      printf("Invalid id\n");
      return;
   } 
   id = mtool_create((int)mt_id, &src, &dst);
   printf("The mtool id is %d\n", id);
}


BOLT(mt_append, "mtool <id> append <src> <dest> <flags> <length> <number>")
{
   uint32_t mt_id, num;
   uint16_t src, dest, flags, len;
   int ret;
   if (!str2u32(pop_argz(), &num)){
      printf("invalid number\n");
      return;
   }
   if (!str2u16(pop_argz(), &len)){
      printf("invalid length\n");
      return;
   }
   flags = mtool_str2flags(pop_argz());
    if (!str2u16(pop_argz(), &dest)){
      printf("invalid dest port\n");
      return;
   }   
   if (!str2u16(pop_argz(), &src)){
      printf("invalid source port\n");
      return;
   } 
   if (!str2u32(pop_argz(), &mt_id)){
      printf("invalid id\n");
      return;
   } 
	ret = mtool_append((int)mt_id, src, dest, 
      flags, len, num);
   printf("The append ret is %d\n", ret);
}





